package com.zzh.lib.views;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.Random;

/**
 * Created by ZZH on 2023/2/16.
 *
 * @date: 2023/2/16 13:40
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.views
 */
public class HLoveHeartView extends RelativeLayout {
    private Context mContext;
    private float[] num = new float[]{-35f, -25f, 0f, 25f, 35f};
    private Drawable zhLove;
    private int zhDoubleInterval = 300;

    public HLoveHeartView(Context context) {
        super(context);
        this.mContext = context;
        initAttrs(null);
    }

    public HLoveHeartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initAttrs(attrs);
    }

    public HLoveHeartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initAttrs(attrs);
    }

    public HLoveHeartView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
        initAttrs(attrs);
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray mTypedArray = mContext.obtainStyledAttributes(attrs, R.styleable.HLoveHeartView);
        zhLove = mTypedArray.getDrawable(R.styleable.HLoveHeartView_zh_love);
        zhDoubleInterval = mTypedArray.getInt(R.styleable.HLoveHeartView_zh_double_interval, 300);
        mTypedArray.recycle();
    }

    float eventX = 0f;
    float eventY = 0f;

    public boolean onTouchEvent(MotionEvent event) {
        if (zhLove == null || zhDoubleInterval <= 0) {
            return super.onTouchEvent(event);
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                eventX = event.getX();
                eventY = event.getY();
            }
            break;
        }
        return super.onTouchEvent(event);
    }

    public void showHeartAnim() {
        showHeartAnim(eventX, eventY);
    }

    public void showHeartAnim(float x, float y) {
        //点击是触发心形的图片add到整个view中，然后执行动画
        //有连续触摸的时候，创建一个展示心形的图片
        ImageView iv = new ImageView(mContext);

        //设置展示的位置，需要在手指触摸的位置上方，即触摸点是心形的右下角的位置
        LayoutParams lp = new LayoutParams(300, 300);
        lp.leftMargin = (int) (x - 150F);
        lp.topMargin = (int) (y - 300F);
        //设置图片资源
        iv.setImageDrawable(zhLove);
        iv.setLayoutParams(lp);

        //把IV添加到父布局当中
        addView(iv);

        //设置控件的动画
        AnimatorSet animatorSet = new AnimatorSet();
        int anInt = new Random().nextInt(4);
        animatorSet.play(
                        //缩放动画，X轴2倍缩小至0.9倍
                        scaleAni(iv, "scaleX", 2f, 0.9f, 100, 0)
                )
                //缩放动画，Y轴2倍缩放至0.9倍
                .with(scaleAni(iv, "scaleY", 2f, 0.9f, 100, 0))
                //旋转动画，随机旋转角
                .with(rotation(iv, 0l, 0, num[anInt]))
                //渐变透明动画，透明度从0-1
                .with(alphaAni(iv, 0F, 1F, 100, 0))
                //缩放动画，X轴0.9倍缩小至
                .with(scaleAni(iv, "scaleX", 0.9f, 1F, 50, 150))
                //缩放动画，Y轴0.9倍缩放至
                .with(scaleAni(iv, "scaleY", 0.9f, 1F, 50, 150))
                //位移动画，Y轴从0上移至600
                .with(translationY(iv, 0F, -600F, 800, 400))
                //透明动画，从1-0
                .with(alphaAni(iv, 1F, 0F, 300, 400))
                //缩放动画，X轴1至3倍
                .with(scaleAni(iv, "scaleX", 1F, 3f, 700, 400))
                //缩放动画，Y轴1至3倍
                .with(scaleAni(iv, "scaleY", 1F, 3f, 700, 400));


        //开始动画
        animatorSet.start();
        //设置动画结束监听
        animatorSet.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                //当动画结束以后，需要把控件从父布局移除
                removeViewInLayout(iv);
            }
        });
    }

    ObjectAnimator scaleAni(
            View view,
            String propertyName,
            float from,
            float to,
            long time,
            long delayTime
    ) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, propertyName, from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setStartDelay(delayTime);
        ani.setDuration(time);
        return ani;
    }

    ObjectAnimator translationX(
            View view,
            float from,
            float to,
            long time,
            long delayTime
    ) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "translationX", from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setStartDelay(delayTime);
        ani.setDuration(time);
        return ani;
    }

    ObjectAnimator translationY(
            View view,
            float from,
            float to,
            long time,
            long delayTime) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "translationY", from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setStartDelay(delayTime);
        ani.setDuration(time);
        return ani;
    }

    ObjectAnimator alphaAni(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "alpha", from, to);
        ani.setInterpolator(new LinearInterpolator());
        ani.setStartDelay(delayTime);
        ani.setDuration(time);
        return ani;
    }

    ObjectAnimator rotation(View view, Long time, long delayTime, float... values) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(view, "rotation", values);
        ani.setDuration(time);
        ani.setStartDelay(delayTime);
        ani.setInterpolator(input -> input);
        return ani;
    }
}
